package org.xm.DeathCommands;

import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Statistic;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;

import java.text.SimpleDateFormat;
import java.util.Date;

public class DeathCommands extends JavaPlugin implements Listener {
    public void onEnable() {
        int pluginId = 18252;
        Metrics metrics = new Metrics(this, pluginId);
        saveDefaultConfig();
        getServer().getPluginManager().registerEvents(this, (Plugin) this);
    }

    @EventHandler
    public void onPlayerDeath(PlayerDeathEvent e) {
        String command_console_cd = getConfig().getString("Console-Command-Cd");
        String player_console_cd = getConfig().getString("Player-Command-Cd");
        Player player = e.getEntity();
        for (String cmd : getConfig().getStringList("Console-Commands"))
            //创建Bukkit调度管理器，并延迟
            Bukkit.getScheduler().runTaskLater(this, new Runnable() {
                public void run() {
                    Bukkit.dispatchCommand(getServer().getConsoleSender(), translatePlaceholders(cmd, player, e ));
                }
            }, Long.parseLong(command_console_cd));

        for (String cmd : getConfig().getStringList("Player-Commands")) {
            //创建Bukkit调度管理器，并延迟
            Bukkit.getScheduler().runTaskLater(this, new Runnable() {
                public void run() {
                    Bukkit.dispatchCommand( e.getEntity(), translatePlaceholders(cmd, player, e));
                }
            }, Long.parseLong(player_console_cd));
        }
    }


    private String translatePlaceholders(String cmd, Player player, PlayerDeathEvent e ) {
        Location location = player.getLocation();
        int deathNumber = player.getStatistic(Statistic.DEATHS);
        Date day=new Date();
        String date = getConfig().getString("Date");
        SimpleDateFormat df = new SimpleDateFormat(date);
        return cmd.replaceAll("%player_name%", player.getName())
                .replaceAll("%x%", Integer.toString(location.getBlockX()))
                .replaceAll("%y%", Integer.toString(location.getBlockY()))
                .replaceAll("%z%", Integer.toString(location.getBlockZ()))
                .replaceAll("%world%", location.getWorld().getName())
                .replaceAll("%DeathMsg%", e.getDeathMessage())
                .replaceAll("%DeathNumber%", Integer.toString(deathNumber))
                .replaceAll("%Deathdate%", df.format(day))
                .replaceAll("&", "§");
    }
    
    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        if (command.getName().equalsIgnoreCase("codm")) {
            if (args.length == 0) {
                sender.sendMessage("/codm reload 重载配置文件");
                return true;
            }
            if (args[0].equalsIgnoreCase("reload")) {
                Plugin config = DeathCommands.getProvidingPlugin(DeathCommands.class);
                config.reloadConfig();
                sender.sendMessage("重载成功");
                return true;
            }
        }
        return false;
    }
}
